package mya.mes.mdc.module.rest;

import com.fasterxml.jackson.databind.JsonNode;
import mya.common.jaxrs.op.OpAdd;
import mya.common.jaxrs.op.OpDelete;
import mya.common.jaxrs.op.OpGet;
import mya.common.jaxrs.op.OpUpdate;
import mya.common.jaxrs.result.AddResult;
import mya.common.jaxrs.result.ChangeResult;
import mya.common.jaxrs.result.DeleteResult;
import mya.common.jaxrs.result.ListResult;

import javax.ws.rs.*;

/**
 * 单表的增删改查CRUD
 *
 * @author leixu
 */
@Path("/{tenantId}/{loginId}/mdc/a/{entity}")
public class MdcEntityResources_ {

    final String OBJECT_ID = "objectId";

    @PathParam("tenantId")
    private String tenantId;

    @PathParam("loginId")
    private Long loginId;

    @PathParam("entity")
    private String entityName;

    /**
     * 新增或者修改一条或者多条记录
     *
     * @param jsonNode
     * @return
     */
    @POST
    @Consumes({"application/json"})
    @Produces({"application/json"})
    public AddResult createOrUpdate(final JsonNode jsonNode) {
        OpAdd opAdd = new OpAdd(tenantId, loginId);

        return opAdd.result(entityName, jsonNode);
    }

    /**
     * 修改一条或者多条记录
     *
     * @param jsonNode
     * @return
     */
    @PUT
    @Consumes({"application/json"})
    @Produces({"application/json"})
    public ChangeResult edit(@PathParam("objectId") final Long objectId, final JsonNode jsonNode) {

        OpUpdate opUpdate = new OpUpdate(tenantId, loginId);
        return opUpdate.result(entityName, jsonNode);

    }

    @PUT
    @Path("/{objectId}")
    @Consumes({"application/json"})
    @Produces({"application/json"})
    public ChangeResult editOneRecord(@PathParam("objectId") final Long objectId, final JsonNode jsonNode) {

        OpUpdate opUpdate = new OpUpdate(tenantId, loginId);
        return opUpdate.result(entityName, jsonNode);

    }

    /**
     * 删除一条多条记录
     * <p>
     * 设置记录中的删除标识：<code>setActive(false)</code></p>
     *
     * @param objectId
     * @param jsonNode
     * @return
     */
    @DELETE
    @Path("/{objectId}")
    @Produces({"application/json"})
    public DeleteResult removeOneRecord(@PathParam("objectId") final Long objectId, final JsonNode jsonNode) {
        OpDelete opDelete = new OpDelete(tenantId, loginId);

        return opDelete.result(entityName, objectId, false);
    }

    /**
     * 从数据库中真实删掉一条记录，</p>
     * <p>
     * 注意：如果有外键关联，会级联删除，注意避免删除不应该删除的数据</p>
     *
     * @param objectId
     * @param jsonNode
     * @return
     */
    @DELETE
    @Path("/true/{objectId}")
    @Produces({"application/json"})
    public DeleteResult removeOneRecordActual(@PathParam("objectId") final Long objectId, final JsonNode jsonNode) {
        OpDelete opDelete = new OpDelete(tenantId, loginId);

        return opDelete.result(entityName, objectId, true);

    }

    /**
     * 删除一条或者多条记录:<code>setActive(false)</code>
     * <p>
     * 数据实际还保存在数据库中，只是ACTIVE_FLAG = 0 (false)</p>
     *
     * @param jsonNode
     * @return
     */
    @DELETE
    @Consumes({"application/json"})
    @Produces({"application/json"})
    public DeleteResult remove(final JsonNode jsonNode) {

        OpDelete opDelete = new OpDelete(tenantId, loginId);

        return opDelete.result(entityName, jsonNode, false);
    }

    /**
     * 从数据库中实际删除一条或者多条记录:<code>em.remove()</code>
     * <p>
     * 从数据库中真实删掉一条记录，</p>
     * <p>
     * 注意：如果有外键关联，会级联删除，注意避免删除不应该删除的数据</p>
     *
     * @param jsonNode
     * @return
     */
    @DELETE
    @Path("/true")
    @Consumes({"application/json"})
    @Produces({"application/json"})
    public DeleteResult removeTrue(final JsonNode jsonNode) {

        OpDelete opDelete = new OpDelete(tenantId, loginId);

        return opDelete.result(entityName, jsonNode, true);
    }

    /**
     * 根据主键查询一条记录
     *
     * @param objectId
     * @return
     */
    @GET
    @Path("/{objectId}")
    @Consumes({"application/json"})
    @Produces({"application/json"})
    public ListResult find(@PathParam("objectId") final Long objectId) {

        OpGet opGet = new OpGet(tenantId, loginId);

        return opGet.result(entityName, objectId);
    }

    @GET
    @Produces({"application/json"})
    public ListResult findAll(
            @QueryParam("q") String qParam,
            @QueryParam("filter") final String filterParam,
            @QueryParam("sort") final String sortParam,
            @QueryParam("start") final Integer start,
            @QueryParam("limit") final Integer limit) {

        OpGet opGet = new OpGet(tenantId, loginId);

        return opGet.result(entityName, qParam, filterParam, sortParam, start, limit);
    }

}
